/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include "../test_db2_comm.h"
#include <db2/db2_core.h>
#include <db2/db2_cmd_obj.h>
#include <db2/config.h>

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x21\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31\xff",
                 49);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_FALSE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "select bigint from sqlj.apm_test1")

    frfree(p_sql);
    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case2(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x21\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31\x01",
                 49);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_FALSE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case3(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x21\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31\x00",
                 49);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_FALSE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case4(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x21\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31",
                 48);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_TRUE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case5(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x21\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31",
                 48);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_FALSE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case6(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x23\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31\xff",
                 49);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_FALSE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case7(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    int i_obj_cp = 0;
    char *p_sql = NULL;

    p_sql = (char *) frmalloc(40);
    snprintf(p_sql, 40, "%s", "select bigint from sqlj.apm_test1");

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_db2_private *p_st_private = (L7session_db2_private *) frct_get_session_private(p_st_session);

    /* 设置数据 */
    p_st_private->p_st_req = db2_create_request();
    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    p_st_private->p_st_fragment->i_correlator_flg = DB2_TRUE;
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x00\x31\xd0\x43\x00\x01\x00\x2b\x24\x14\x00\x00\x00\x00\x21\x73\x65\x6c\x65\x63\x74\x20\x62\x69\x67\x69\x6e\x74\x20\x66\x72\x6f\x6d\x20\x73\x71\x6c\x6a\x2e\x61\x70\x6d\x5f\x74\x65\x73\x74\x31\xff",
                 49);
    bytebuff_setPosition(p_st_private->p_st_fragment->p_st_data, p_st_private->p_st_fragment->u64_parsed_index);
    i_ret = db2_get_obj_cp(p_st_private->p_st_fragment, DB2_FALSE, &i_obj_cp, &p_st_private->p_st_req->ui_ddm_len);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_obj_cp, DRDA_CP_SQLSTT);

    i_ret = db2_get_sqlstt(p_st_private->p_st_fragment, DB2_FALSE, &p_sql);
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "select bigint from sqlj.apm_test1;select bigint from sqlj.apm_test1")

    frfree(p_sql);
    test_db2_destroy_ct(&p_st_conn);

    return;
}

int add_suite_db2_get_sqlstt(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" db2_get_sqlstt()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
